package com.example.shiyun;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.shiyun.UI.Guide_Page;
import com.example.shiyun.UI.Index;
import com.example.shiyun.Util.Util;


/**
 * 倒计时欢迎页
 */
public class MainActivity extends AppCompatActivity {
    private TextView mian_skip;
    private boolean isFristUserAPP = false; // 是否是第一次进入程序
    private SharedPreferences share; // 用于保存isFristUserAPP

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //设置沉浸模式
//        Util util = new Util();
//        util.Setimmersion(getWindow());
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        initView();
    }

    private void initView() {
        mian_skip = (TextView) findViewById(R.id.mian_skip);
        // 获取isFristUserAPP的值判断是否是第一次进入程序
        share = this.getSharedPreferences("isFristUserAPP", MODE_PRIVATE);
        isFristUserAPP = share.getBoolean("isFristUserAPP", true);
        new Thread(()->{
            //设置跳转秒数
            for (int i = 3; i >= 0; i--) {
                int finalI = i;
                runOnUiThread(()->{
                    mian_skip.setText(finalI+"");
                });
                try {
                    Thread.sleep(900);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (isFristUserAPP) {
                //判断是否有网络
                if (Util.isNetworkConnected(this))
                {
                    // 进入程序引导页面
                    Intent intent1=new Intent(this, Guide_Page.class);
                    startActivity(intent1);
                    // 保存isFristUserAPP的状态
                    SharedPreferences.Editor editor = share.edit();
                    editor.putBoolean("isFristUserAPP", false);
                    editor.commit();
                    finish();
                }else
                {
                    runOnUiThread(()->{
                        new AlertDialog.Builder(this)
                                .setTitle("提示！")
                                .setMessage("请开启网络后，再次操作！")
                                .setCancelable(false)
                                .setNegativeButton("",null)
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        Util.exitApp(MainActivity.this);
                                    }
                                }).show();
                    });
                }
            }else
            {
                Intent intent=new Intent(this, Index.class);
                startActivity(intent);
                finish();
        }
        }).start();
    }
}